/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.modele;

import librairie.Message;
import librairie.StructureProtocole;
import Exceptions.*;
import exception.CommunicationException;
import exception.CommunicationResponseException;
import exception.ReadingObjectException;
import librairie.InterfaceTransport;

/**
 *
 * @author KB
 */
public class ProtocoleClient implements InterfaceProtocoleClient {

    private InterfaceTransport transport;

    public ProtocoleClient(InterfaceTransport _transport) {
        transport = _transport;
    }

    public void envoyerDemandeAnnuites(Double montant, Double tauxInteret, int nbAnnees) throws CommunicationResponseException {
        try {
            Message messageAnnuite = new Message();
            messageAnnuite.ajouterParametre(StructureProtocole.CHOIX, 1);
            messageAnnuite.ajouterParametre(StructureProtocole.MONTANT, montant);
            messageAnnuite.ajouterParametre(StructureProtocole.NB_ANNEES, nbAnnees);
            messageAnnuite.ajouterParametre(StructureProtocole.TAUX_INTERET, tauxInteret);
            transport.connecter();
            transport.envoyer(messageAnnuite);
        } catch (CommunicationException ce) {
            throw new CommunicationResponseException();
        }
    }

    public void envoyerDemandeNbAnnees(Double montant, Double tauxInteret, Double annuite) throws CommunicationResponseException {
        try {
            Message messageNbAnnees = new Message();
            messageNbAnnees.ajouterParametre(StructureProtocole.CHOIX, 2);
            messageNbAnnees.ajouterParametre(StructureProtocole.MONTANT, montant);
            messageNbAnnees.ajouterParametre(StructureProtocole.ANNUITE, annuite);
            messageNbAnnees.ajouterParametre(StructureProtocole.TAUX_INTERET, tauxInteret);
            transport.connecter();
            transport.envoyer(messageNbAnnees);
        } catch (CommunicationException ce) {
            throw new CommunicationResponseException();
        }
    }

    public double recevoirReponse() throws CommunicationResponseException, ReadingObjectException,
            InputChoixException, InputMontantException,
            InputNbAnException, InputTxInteretException,
            InputAnnuiteException {

        Message m;
        try {
            m = (Message) transport.recevoir();
            transport.fermer();
            //On ne dialogueras plus avec le serveur, on peux donc fermer la socket de communication
            int codeRetour = (Integer) m.recupererParametre(StructureProtocole.CODE_RETOUR);
            if (codeRetour > 0) {
                switch (codeRetour) {
                    case StructureProtocole.ERREUR_CHOIX:
                        throw new InputChoixException();
                    case StructureProtocole.ERREUR_MONTANT:
                        throw new InputMontantException();
                    case StructureProtocole.ERREUR_NBAN:
                        throw new InputNbAnException();
                    case StructureProtocole.ERREUR_TX_INTERET:
                        throw new InputTxInteretException();
                    case StructureProtocole.ERREUR_ANNUITES:
                        throw new InputAnnuiteException();
                }
            }
        } catch (CommunicationException ce) {
            throw new CommunicationResponseException();
        }
        return (Double) m.recupererParametre(StructureProtocole.RESULTAT);
    }
}